VBScripts, Perl scripts are highly useful for automating some of routine tasks. When dealing with data logs, processing data regulary these scripts are highly useful. Regular expressions are the heart of dataprocessing, we cannot imagine data processing without it.
VBScript has got an object called RegExp
Initialize the object with
- Regular Expression Pattern
- Ignore the case (for most cases)
- Select Exhaustive Search
Now search the input text with above Regex Object
Now lets extract id of a megaupload URL
Dim url : url ="www.megaupload.com/watch?v=AXZYQM6" id = extractID( url, "[A-Z]+$") Function extractID( text, pattern ) 'First build regular expression box Dim Regex, Matches Set Regex = new RegExp 'Create Regular Expression object Regex.Pattern = pattern 'Apply pattern to Regex object Regex.Global = True 'Search all the occurances Set Matches = Regex.Execute( text ) 'Execute the text on the regular expression obj and retreive the list Dim i For i = 0 To Matches.Count WScript.Echo Matches(i) Next ExtractMatch = Matches(0) End Function
Now We lets consider a practical example to stripping of HTML elements in a text
We shall you Repace() in RegExp object to replace a text having HTML element pattern with nothing.
HTML elements are like <abcxyz>, so Pattern would be “<+>”.
Function stripHTMLtags(HTMLstring)
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = ""
.IgnoreCase = True
.Global = True
End With
stripHTMLtags = RegularExpressionObject.Replace(HTMLstring, "")
Set RegularExpressionObject = nothing
End Function
Lovely and easy right….just create a regex object and specify pattern then execute it on a text string.
nice